<?php
class SmsKuteNewController extends CpController
{
    public function actionIndex()
    {        
        $this->breadcrumbs[] = array('name'=>'DS SMS Kute Admin', 'link'=>'', 'class'=>'active');
        
        /* lay cac app sms kute */
        $apps = ASmsKuteNew::getApps();
        
        $currentPage = isset($_GET["page"]) ? intval($_GET["page"]) : 1;
        $currentPage = $currentPage==0 ? 1 : $currentPage;
        $numberRecordPerPage = Yii::app()->params["numberRecordPerPage"];
        
        $search = array();
        $search["toDate"] = isset($_GET["toDate"]) ? $_GET["toDate"] : "";
        $search["fromDate"] = isset($_GET["fromDate"]) ? $_GET["fromDate"] : "";
        $search["title"] = isset($_GET["title"]) ? trim(strip_tags($_GET["title"])) : "";        
        $search["price"] = isset($_GET["price"]) ? trim(strip_tags($_GET["price"])) : "";
        $search["status"] = isset($_GET["status"]) ? trim(strip_tags($_GET["status"])) : "";
        $search["eventId"] = isset($_GET["eventId"]) ? intval($_GET["eventId"]) : 0;
        
        $sms = AAppSmsKuteNew::getAllSms($search, $currentPage, $numberRecordPerPage);
        $totalSms = AAppSmsKuteNew::getTotalSms($search);
        $numberPage = ceil($totalSms / $numberRecordPerPage);
        
        $this->render(
            "index"
            , array(
                "search"=>$search
                , "apps"=>$apps
                , "sms"=>$sms
                , "totalSms"=>$totalSms
                , "currentPage"=>$currentPage
                , "numberPage"=>$numberPage
            )
        );
    }
    
    public function actionAdd()
    {        
        /* lay cac app sms kute */
        $apps = ASmsKuteNew::getApps();
        
        $data = array();
        $error = "";
        if(isset($_POST["title"])){
            $data["title"] = trim(strip_tags($_POST["title"]));
            $data["eventId"] = trim(strip_tags($_POST["eventId"]));
            $data["content"] = trim($_POST["content"]);            
            $data["price"] = trim($_POST["price"]);                                                
            
            $error .= empty($data["title"]) ? "<p>- Bạn chưa nhập tiêu đề sms</p>" : "";
            $error .= empty($data["content"]) ? "<p>- Bạn chưa nhập nội dung sms</p>" : "";            
            
            if(empty($error)){
                $data["userId"] = 0;
                $data["username"] = null;
                $data["download"] = 0;
                $data["create_date"] = time();
                $data["update_date"] = time();
                $data["status"] = 1;
                $data["hit"] = 0;
                
                $result = ASmsKuteNew::InsertSmsKute($data);
                if($result>=0){
                    $act = 1;
                    $obj = 28;
                    $content = LogConfig::$action[$act] . ' ' . LogConfig::$object[$obj] . ': {title: ' . $data["title"];
                    $content .= ', eventId: ' . $data["eventId"] . ', price: ' . $data["price"] . ',id: ' . $result . '}';
                    $result = AAdminLog::insertAdminLog($act, $obj, $content);
                    
                    $this->redirect(Url::createUrl("smsKuteNew/index"));
                }
            }
        }


        $this->render(
            "add"
            , array(
                "apps"=>$apps
                , "data"=>$data
                , "error"=>$error
            )
        );   
    }
    
    public function actionEdit()
    {
        /* lay cac app sms kute */
        $apps = ASmsKuteNew::getApps();
        
        $id = isset($_GET["id"]) ? intval($_GET["id"]) : 0;
        $data = ASmsKuteNew::GetSmsInfo($id);
        if(empty($data)){
            throw new CHttpException(404, "Trang yêu cầu không tồn tại");
        }
        
        $error = "";        
        
        if(isset($_POST["title"])){
            $data_edit = array();
            $data_edit["title"] = trim(strip_tags($_POST["title"]));
            $data_edit["eventId"] = trim(strip_tags($_POST["eventId"]));
            $data_edit["content"] = trim($_POST["content"]);            
            $data_edit["price"] = trim($_POST["price"]);                                                
            
            $error .= empty($data_edit["title"]) ? "<p>- Bạn chưa nhập tiêu đề sms</p>" : "";
            $error .= empty($data_edit["content"]) ? "<p>- Bạn chưa nhập nội dung sms</p>" : "";
                        
            
            if(empty($error)){
                $data_edit["id"] = $data["id"];
                $data_edit["update_date"] = time();                                
                $result = ASmsKuteNew::UpdateSms($data_edit);
                
                if($result>=0){
                    $act = 2;
                    $obj = 28;
                    $content = LogConfig::$action[$act] . ' ' . LogConfig::$object[$obj] . ": {title: " . $data["title"] . ", eventId: " . $data["eventId"] . ", price: " . $data["price"] . "} Thành ";
                    $content .= '{title: ' . $data_edit["title"] . ', eventId: ' . $data_edit["eventId"] . ', price: ' . $data_edit["price"] . ', id: ' . $id . '}';
                    $result = AAdminLog::insertAdminLog($act, $obj, $content);                    
                
                    $this->redirect(Url::createUrl("smsKuteNew/index"));
                }                
            }
        }
        
        $this->render(
            "edit"
            , array(
                "error"=>$error
                , "data"=>$data                
                , "apps"=>$apps                
            )
        );   
    }
    
    public function actionAjaxQuickUpdate()
    {        
        $data["status"] = isset($_POST["status"]) ? intval($_POST["status"]) : 0;        
        $data["id"] = isset($_POST["id"]) ? intval($_POST["id"]) : 0;
        
        if($data["id"] >0){   
            $result = ASmsKuteNew::QuickUpdate($data);
            
            if($result>0){
                $act = 2;
                $obj = 28;
                $content = LogConfig::$action[$act] . ' ' . LogConfig::$object[$obj] . ': {smsId: ' . $data["id"] . '} thành ';
                $content .= '{smsId: ' . $data["id"] . ',status: ' . $data["status"] . '}';
                $result = AAdminLog::insertAdminLog($act, $obj, $content);                  
                echo 1;exit();
            }else{
                echo "Chưa thay đổi dữ liệu";
            }
        }
    }
    
    public function actionAjaxDelete()
    {        
        $id = isset($_POST["id"]) ? intval($_POST["id"]) : 0;
        if($id >0){
            $result = ASmsKuteNew::DeleteSms($id);
            if($result >0){
                $act = 3;
                $obj = 28;
                $content = LogConfig::$action[$act] . " " . LogConfig::$object[$obj] . ": {smsId: " . $id . "}";
                $result = AAdminLog::insertAdminLog($act, $obj, $content);                  
                echo 1;exit();
            } else {
                echo "Chưa thay đổi dữ liệu";
            }
        }
    }
}
?>